import { _decorator, Component, director, Node } from "cc"
import ResManager from "./framework/ResManager"
import { resources } from "cc"
import { Prefab } from "cc"
import { instantiate } from "cc"
import { JsonAsset } from "cc"
import { SpriteFrame } from "cc"
import { AudioClip } from "cc"
import { UITransform } from "cc"
import { Tween } from "cc"
const { ccclass, property } = _decorator
const ResourceBundle = {
  textures: {
    assetsType: SpriteFrame,
    assetsList: [],
  },
  audios: {
    assetsType: AudioClip,
    assetsList: [],
  },
  configs: {
    assetsType: JsonAsset,
    assetsList: ["configs/levelData", "configs/monsterData", "configs/roleData"],
  },
  prefabs: {
    assetsType: Prefab,
    assetsList: [
      "prefabs/game/role/role1",
      "prefabs/game/role/monster1",
      "prefabs/game/role/monster2",
      "prefabs/game/role/monster3",
      "prefabs/game/gameObject/nodeRewardCoin",
      "prefabs/game/gameObject/nodeHp",
      "prefabs/game/gameObject/guideHand",
      "prefabs/gameView",
      "prefabs/endingView",
      "prefabs/game/effect/effect_role_out"
    ],
  },
}

@ccclass("loading")
export class loading extends Component {
  private totalProgress: number = 0
  private currProgress: number = 0
  private currLoadIndex: number = 0
  private loadResTypes: any = []

  @property(UITransform)
  uiTransform: UITransform = null

  @property(Node)
  nodeUIRole: Node = null

  private maxProgressWidth: number = 0

  start() {
    this.currLoadIndex = 0
    this.loadResTypes = [ResourceBundle.audios, ResourceBundle.configs, ResourceBundle.prefabs, ResourceBundle.textures]
    for (let i: number = 0; i < this.loadResTypes.length; i++) {
      let list = this.loadResTypes[i].assetsList
      this.totalProgress += list.length
    }
    console.log("totalProgress:", this.totalProgress)
    this.maxProgressWidth = this.uiTransform.width
    this.uiTransform.width = 0
    this.nodeUIRole.positionX = -this.maxProgressWidth / 2
    this.onProgressLoadSuccess()
    this.startLoadRes()
  }

  private startLoadRes() {
    if (this.currLoadIndex < this.loadResTypes.length) {
      this.loadResDir(this.loadResTypes[this.currLoadIndex], () => {
        this.currLoadIndex++
        this.startLoadRes()
      })
    } else {
      this.onLoadBundlesSuccess()
    }
  }

  private loadResDir(resInfo: any, onComplete: Function) {
    let resType = resInfo.assetsType
    let requests = resInfo.assetsList
    let maxProgress = Object.keys(requests).length
    if (maxProgress == 0) {
      onComplete && onComplete()
      return
    }
    let progress = 0

    for(let key in requests){
      ResManager.instance.loadRes(requests[key], resType, () => {
        ++progress
        ++this.currProgress
        this.onProgressLoadSuccess()
        if (progress >= maxProgress) {
          onComplete && onComplete()
        }
      })
    }
  }

  onProgressLoadSuccess() {
    // console.log("progress => ", this.currProgress, this.totalProgress)
    const width = (this.maxProgressWidth * this.currProgress) / this.totalProgress
    Tween.stopAllByTarget(this.uiTransform)
    new Tween(this.uiTransform).to(0.1, { width: width }).start()
    Tween.stopAllByTarget(this.nodeUIRole)
    new Tween(this.nodeUIRole).to(0.1, { positionX: -this.maxProgressWidth / 2 + width }).start()
  }

  onLoadBundlesSuccess() {
    console.log("资源加载成功")
    this.initGameView()
  }
  onLoadBundlesFail(error) {
    console.log("资源加载失败", error)
  }

  protected initGameView(): void {
    // return
    const canvasNode = director.getScene().getChildByName("Canvas")
    const gameViewPrefab = resources.get("prefabs/gameView", Prefab)
    const gameView = instantiate(gameViewPrefab)
    this.scheduleOnce(() => {
      const nodeLoading = canvasNode.getChildByName("nodeLoading")
      if (nodeLoading) {
        nodeLoading.destroy()
      }
      gameView.setParent(canvasNode)
    }, 0.1)
  }
}
